package com.kly.cashmall.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;

import com.gyf.immersionbar.ImmersionBar;
import com.kly.cashmall.R;
import com.kly.cashmall.framework.module.BaseFragment;
import com.kly.cashmall.utils.system.BarUtils;


/**
 * @author
 * @date 2017/12/22
 */
public abstract class BaseBarFragment extends BaseFragment {

    protected @LayoutRes
    int getActionBarLayoutId() {
        return R.layout.layout_center_toolbar;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup,
                             Bundle savedInstanceState) {
        View rootView;
        if ((rootView = getView()) == null) {
            rootView = inflater.inflate(R.layout.fragment_base_content, viewGroup, false);
            onReplaceActionBar(inflater, rootView);
        }
        View.inflate(getActivity(), getContentViewId(),
                (ViewGroup) rootView.findViewById(R.id.content_container));
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        return rootView;
    }

    protected View onReplaceActionBar(LayoutInflater inflater, View rootView) {
        FrameLayout container = (FrameLayout) rootView.findViewById(R.id.action_bar_container);
        container.setVisibility(View.VISIBLE);
        View actionBar = inflater.inflate(getActionBarLayoutId(), container, false);
        container.addView(actionBar);
        BarUtils.setActionBarLayoutFragment(actionBar);
        return rootView;
    }

    /**
     * 设置标题位置
     *
     * @param gravity
     */
    public void setTitleGravity(int gravity) {
        ((TextView) bindView(R.id.action_title)).setGravity(gravity);
    }

    /**
     * 设置导航是否显示
     *
     * @param visibility
     */
    public void setToolBarVisible(boolean visibility) {

        bindView(R.id.toolbar, visibility);
    }

    public void setTitleVisible(boolean visibility) {
        bindView(R.id.action_title, visibility);
    }

    /**
     * 客服是否显示
     */
    public void setToolbarCall(boolean visible,int drawableID ,View.OnClickListener listener) {
        bindView(R.id.action_call, visible);
        bindView(R.id.action_call, listener);
        ((ImageView) bindView(R.id.action_call)).setImageResource(drawableID);
    }

    /**
     * 设置标题背景
     */

    public void setToolBarBackground(int ResId){
        bindView(R.id.toolbar).setBackgroundResource(ResId);
    }
    /**
     * 设置标题
     */
    public void setTitle(CharSequence charSequence) {
        bindText(R.id.action_title, charSequence);
    }
    public void setTitleColor(CharSequence charSequence,int colorId) {
        bindText(R.id.action_title, charSequence);
        ((TextView) bindView(R.id.action_title)).setTextColor(colorId);
    }

    public void setTitle(@StringRes int id) {
        setTitle(getString(id));
    }

    /**
     * 设置消息入口
     *
     * @param
     */
    public void setActionBarMessage(boolean visibility, boolean dotVisibility, View.OnClickListener listener) {
        bindView(R.id.toolbar_message, visibility);
        bindView(R.id.toolbar_message_dot, dotVisibility);
        bindView(R.id.toolbar_message, listener);
    }


    /**
     * 设置导航是否显示
     *
     * @param visibility
     */
    public void setActionBar(boolean visibility) {
        bindView(R.id.toolbar, visibility);
    }

}
